﻿using PagedList;
using PagedList.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GiaoTrinhEPU.Repositories.Repositories;
using GiaoTrinhEPU.Repositories.DataModels;
using GiaoTrinhEPU.Repositories;
using GiaoTrinhEPU.Repositories.Interfaces;
using GiaoTrinhEPU.Core.ActionResults;
using GiaoTrinhEPU.Core;

namespace GiaoTrinhEPU.Controllers
{
    public class DegreeController : BaseController
    {
        /// <summary>
        /// Declare Degree Repository
        /// </summary>
        private DegreeRepository degreeRepository;

        /// <summary>
        ///  Declare Session
        /// </summary>
        public static string SessionDegreeSort = "SessionDegreeSort";
        public static string SessionDegreeSearch = "SessionDegreeSearch";
        public static string SessionDegreeSortOpt = "SessionDegreeSortOpt";



        // GET: /Degree/
        /// <summary>
        /// Constructor that allow to create a Comment controller
        /// </summary>
        public DegreeController()
            : base()
        {
            this.degreeRepository = new DegreeRepository();
            ViewBag.SelectedMenuId = 25;
        }

        public ActionResult Index(int page = 1, int size = 25, String search = "", String opt = "", String sort = "")
        {
            ViewBag.Title = "Hệ Đào Tạo";
            // Stored Session

            Session[SessionDegreeSort] = sort.Trim().Length > 0 ? sort.ToLower() : Session[SessionDegreeSort];
            Session[SessionDegreeSortOpt] = opt.Trim().Length > 0 ? opt.ToLower() : Session[SessionDegreeSortOpt];

            var degree = this.degreeRepository.ListDegrees(false).ToList();

            // Search
            if (Session[SessionDegreeSearch] != null)
            {
                ViewBag.Keyword = Session[SessionDegreeSearch].ToString();

                degree = degree.Where(l => l.Name.ToUpper().Contains(ViewBag.Keyword.ToUpper())).ToList();

            }
            // Sort 
            ViewBag.SortNameOpt = "desc";
            if (Session[SessionDegreeSort] != null)
            {
                switch (Session[SessionDegreeSort].ToString())
                {
                    case "name":
                        degree = Session[SessionDegreeSortOpt].ToString().Equals("desc") ? degree.OrderByDescending(s => s.Name).ToList() : degree.OrderBy(s => s.Name).ToList();

                        Session[SessionDegreeSort] = "name";
                        ViewBag.SortNameOpt = Sort.SetSortOrderOption(Session[SessionDegreeSortOpt].ToString());
                        break;

                    default:
                        break;
                }
            }


            return View(degree.ToPagedList(page, size));
        }
    }
}